let util = {}

// base64编码，配合encodeURIComponent使用
util.base64Encode = function(input) {
	var c1, c2, c3;
	var base64EncodeChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var i = 0,
		len = input.length,
		strin = '';
	while (i < len) {
		c1 = input.charCodeAt(i++) & 0xff;
		if (i == len) {
			strin += base64EncodeChars.charAt(c1 >> 2);
			strin += base64EncodeChars.charAt((c1 & 0x3) << 4);
			strin += "==";
			break;
		}
		c2 = input.charCodeAt(i++);
		if (i == len) {
			strin += base64EncodeChars.charAt(c1 >> 2);
			strin += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
			strin += base64EncodeChars.charAt((c2 & 0xF) << 2);
			strin += "=";
			break;
		}
		c3 = input.charCodeAt(i++);
		strin += base64EncodeChars.charAt(c1 >> 2);
		strin += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
		strin += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
		strin += base64EncodeChars.charAt(c3 & 0x3F)
	}
	return strin
}

// base64解码，配合decodeURIComponent使用
util.base64Decode = function(input) {
	var base64EncodeChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	while (i < input.length) {
		enc1 = base64EncodeChars.indexOf(input.charAt(i++));
		enc2 = base64EncodeChars.indexOf(input.charAt(i++));
		enc3 = base64EncodeChars.indexOf(input.charAt(i++));
		enc4 = base64EncodeChars.indexOf(input.charAt(i++));
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;
		output = output + String.fromCharCode(chr1);
		if (enc3 != 64) {
			output = output + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
			output = output + String.fromCharCode(chr3);
		}
	}
	return utf8_decode(output);
}

function utf8_decode(utftext) { // utf-8解码
	var string = '';
	let i = 0;
	let c = 0;
	let c1 = 0;
	let c2 = 0;
	while (i < utftext.length) {
		c = utftext.charCodeAt(i);
		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		} else if ((c > 191) && (c < 224)) {
			c1 = utftext.charCodeAt(i + 1);
			string += String.fromCharCode(((c & 31) << 6) | (c1 & 63));
			i += 2;
		} else {
			c1 = utftext.charCodeAt(i + 1);
			c2 = utftext.charCodeAt(i + 2);
			string += String.fromCharCode(((c & 15) << 12) | ((c1 & 63) << 6) | (c2 & 63));
			i += 3;
		}
	}
	return string;
}


util.formatTime = function(date) {
	var date = new Date()
	var year = date.getFullYear()
	var month = date.getMonth() + 1
	var day = date.getDate()

	var hour = date.getHours()
	var minute = date.getMinutes()
	var second = date.getSeconds()

	return [year, month, day].map(formatNumber).join('-') + '-' + [hour, minute, second].map(formatNumber).join('-')
}

/**
 * 计算两个经纬度的距离(千米)
 */
util.getDistance = function(lat1, lng1, lat2, lng2) {
	
	if (!(lat1 && lat2 && lng1 && lng2)){
		return ''
	}
	
	var radLat1 = lat1 * Math.PI / 180.0;
	var radLat2 = lat2 * Math.PI / 180.0;
	var a = radLat1 - radLat2;
	var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
		Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
	s = s * 6378137; // EARTH_RADIUS;
	if (s > 100) {
		if (s > 100000) {
			s = '>100km'
		}else{
			s = (s/1000.0).toFixed(1) + 'km'
		}
	}else {
		s = Math.floor(s) + 'm'
	}
	return s;
}

// 数字转字符串
function formatNumber(n) {
	n = n.toString()
	return n[1] ? n : '0' + n
}

util.formatNumber = function(n) {
	n = n.toString()
	return n[1] ? n : '0' + n
}

// 金额分转元
util.getPrice = function(price) {
	if (typeof parseInt(price) == NaN) {
		return 0.00;
	}
	return (parseInt(price) / 100.0).toFixed(2)
}

// 生成随机颜色
util.randomColor16 = function() { //十六进制颜色随机
	var r = Math.floor(Math.random() * 256);
	var g = Math.floor(Math.random() * 256);
	var b = Math.floor(Math.random() * 256);
	var color = '#' + r.toString(16) + g.toString(16) + b.toString(16);
	return color;
}

util.systemRect = function() {
	let systemInfo = uni.getSystemInfoSync();
	console.log(systemInfo)

	var rect = {
		screenWidth: systemInfo.screenWidth,
		screenHeight: systemInfo.screenHeight,
		windowWidth: systemInfo.windowWidth,
		windowHeight: systemInfo.windowHeight,
		statusBarHeight: systemInfo.statusBarHeight,
		safeAreaTopHeight: systemInfo.safeAreaInsets.top,
		safeAreaBottomHeight: systemInfo.screenHeight - systemInfo.windowHeight + systemInfo.safeAreaInsets.bottom
	}

	return rect
}

util.JSEncode = function(string){
	if (string.length <= 0) {
		return ''
	}
	var dataObj = JSON.parse(string)
	if ((typeof dataObj) != 'object') {
		return ''
	}
	var dataArray = []
	for (var key in dataObj) {
		let value = dataObj[key]
		
		dataArray.push(key + '=' + value)
	}
	return dataArray.join('@')
}

util.JSDecode = function(string){
	if (string.length <= 0 && (typeof string) != 'string') {
		return {}
	}
	var dataArray = string.split('@')
	var dataObj = new Object()
	dataArray.forEach(function(item, index){
		let itemArray = item.split('=')
		if (itemArray.length != 2) {
			
		}else{
			dataObj[itemArray[0]] = itemArray[1]
		}
	})
	return dataObj
}

export default util
